package com.itheima.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class MapperUtils {
    private static SqlSession sqlSession=null;

    //获取代理对象
    public static <T> T getMapper(Class<?> className) {
        //SqlSession sqlSession=null;
        try {
            InputStream is = Resources.getResourceAsStream("sqlConfig.xml");
            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
            sqlSession = factory.openSession(true);
        } catch (IOException e) {
            e.printStackTrace();
        }
        T mapper = (T) sqlSession.getMapper(className);
        return mapper;
    }
    //释放资源
    public static void close(){
        if(sqlSession!=null){
            sqlSession.close();
        }
    }
}
